package com.vincce.sport.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.SimpleStorageClient;


@Controller
@RequestMapping("/img")
public class ImgUpController extends BaseController{
    
	@RequestMapping("/upload")
	@ResponseBody 
	public String imaUpload(Model model, String fileType, InputStream is) throws IOException{
		Boolean flag = false;
		String msg = "";
        Map<String, Object> result = new HashMap<String, Object>();
        SimpleStorageClient.StorageResult obj = new SimpleStorageClient.StorageResult(200, "");
        if (is != null && fileType != null) {
            if (!fileType.equals(".jpg") && !fileType.equals(".JPG") && !fileType.equals(".png") && !fileType.equals(".PNG")) {
                msg = "格式不正确,只支持后缀名为.jpg或者.png的图片上传。";
            } else {
                obj = SimpleStorageClient.upload(2, is, fileType);
                if (obj.statusCode == 200) {
                	flag = true;
                	String path = obj.info.replaceAll("\\\\", "/");
                	result.put("img", String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+path, "ori"));
                	result.put("imgPath", path);
                } else{
                	msg = obj.info.replaceAll("\\\\", "/");
                }
            }
        } else{
        	msg = "请选择图片!";
        }
        result.put("suc", flag);
        result.put("msg", msg);
        return JSON.toJSONString(result);
	}

}
